package id.net.iconpln.bb.server.dao;

import id.net.iconpln.bb.server.util.CommonModule;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import oracle.jdbc.OracleTypes;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class MasterDao {
	@Autowired private SessionFactory sessionFactory;
	
	public List<Map> getMaster(String kode, Map map){
		List<Map> lMap = new ArrayList<Map>();
		Session session = sessionFactory.getCurrentSession();
        Connection con = session.connection();
        System.out.println("masuk get master dao");
        System.out.println("kode master: " + kode);
        String sql = null;
        CallableStatement call;
        try {
                        
            if (kode.equals("KAPAL")){
            	sql = "{call bbtrx.SETID_TRANSPORTIR(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_transportir").toString());
                call.execute();
            }else if (kode.equals("SURVEYORBYPEMASOK_PENGAJUAN")){
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pltu").toString());
                call.execute();
                
                sql = "{call bbtrx.SETID_PEMASOK(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pemasok").toString());
                call.execute();
            }else if (kode.equals("KONTRAK_PLTU")){
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pltu").toString());
                call.execute();
                
                sql = "{call bbtrx.SETID_PEMASOK(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pemasok").toString());
                call.execute();
            }else if (kode.equals("TRANSPORTIR_BY_PEMASOK") || kode.equals("KIT_BY_PEMASOK") || 
            		kode.equals("TRANSPORTIR_BY_PEMASOK_OUT") || kode.equals("KIT_BY_PEMASOK_OUT")){
            	sql = "{call bbtrx.SETID_PEMASOK(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pemasok").toString());
                call.execute();
            }else if (kode.equals("UNIT")){
            	
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pltu").toString()); //CALL FROM SESSION USER LOGIN AFTER LOGIN IMPLENT
                call.execute();
                               
            	System.out.println("out = " + sql +" " + map.get("id_pltu").toString());
            }else if (kode.equals("SURVEYOR")){
            	
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, "2"); //CALL FROM SESSION USER LOGIN AFTER LOGIN IMPLENT
                call.execute();
                               
            	System.out.println("out = " + sql + "kode :" + kode);            	
            }else if (kode.equals("JETTY")){
            	
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, "2"); //CALL FROM SESSION USER LOGIN AFTER LOGIN IMPLENT
                call.execute();
                               
            	System.out.println("out = " + sql);   
            }else if (kode.equals("PEMASOK_BY_PLTU")){
            	
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pltu").toString()); //CALL FROM SESSION USER LOGIN AFTER LOGIN IMPLENT
                call.execute();
            }else if (kode.equals("PEMASOK_BY_PLTU_RAKOR")){
            	
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pltu").toString()); 
                call.execute();
                
                sql = "{call bbtrx.SETNO_RAKOR(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("no_rakor").toString()); 
                call.execute();
            }else if (kode.equals("PEMASOK_BY_PLTU_OUT")){
            	
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pltu").toString()); //CALL FROM SESSION USER LOGIN AFTER LOGIN IMPLENT
                call.execute();
            }else if (kode.equals("SURVEYOR_BY_PLTU") || kode.equals("SURVEYOR_BY_PLTU_OUT")){
            	
            	sql = "{call bbtrx.SETID_PLTU(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("id_pltu").toString()); 
                call.execute();
            }else if (kode.equals("PEMASOKBYSTATUS")){
            	
            	sql = "{call bbtrx.SETSTATUS_PEMASOK(?)}";
                call = con.prepareCall(sql);
                call.setString(1, map.get("status_pemasok").toString()); 
                call.execute();
            }
            
            sql = "{? = call BBTRX.GETLISTMASTERBB(?)}";
            call = con.prepareCall(sql);
            call.registerOutParameter(1, OracleTypes.CURSOR);
            call.setString(2, kode);
            call.execute();
            ResultSet rset = (ResultSet) call.getObject(1);
            lMap = CommonModule.convertResultsetToListLower(rset);
        }catch (Exception e) {
			e.printStackTrace();
		}
        
        return lMap;
	}
	
	public List<Map> setRelationMaster(String kode, Map map){
		List<Map> lMap = new ArrayList<Map>();
		Session session = sessionFactory.getCurrentSession();
        Connection con = session.connection();
        String sql = null;
        CallableStatement call;
        try {
        	sql = "{call bbtrx.SETID_PLTU(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("id_pltu").toString());
            call.execute();
            
            sql = "{call bbtrx.SETID_PEMASOK(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("id_pemasok").toString());
            call.execute();
            
            sql = "{call bbtrx.SETID_SURVEYOR(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("id_surveyor").toString());
            call.execute();
            
            sql = "{call BBTRX.SETCREATEBY(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("user").toString());
            call.execute();
            
            sql = "{call BBTRX.SETAPP_ACCESS(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("app_access").toString());
            call.execute();
            
            sql = "{? = call BBTRX.SETRELATIONMASTER(?, ?)}";
            call = con.prepareCall(sql);
            call.registerOutParameter(1, OracleTypes.VARCHAR);
            call.setString(2, kode);
            call.setString(3, map.get("crud").toString());       
            call.execute();
            
            map.put("message", call.getString(1));    
            System.out.println(call.getString(1));
        }catch (Exception e) {
        	map.put("message", e.getMessage());    
        	e.printStackTrace();
		}
        lMap.add(map);        
        return lMap;
	}

	public List<Map> crudMaster(String kode, Map map){
		List<Map> lMap = new ArrayList<Map>();
		Session session = sessionFactory.getCurrentSession();
        Connection con = session.connection();
        String sql = null;
        CallableStatement call;
        try {
        	
        	if (map.get("crud").equals("UPDATEPERINGATAN")){
		            sql = "{call bbtrx.SETSTATUS_PEMASOK(?)}";
		            call = con.prepareCall(sql);
		            call.setString(1, map.get("status_pemasok").toString());
		            call.execute();
            }else if  (map.get("crud").equals("UPDATEBLACKLIST")){      

            	sql = "{call bbtrx.SETFLAG_BLACKLIST(?)}";
	            call = con.prepareCall(sql);
	            call.setString(1, map.get("status_pemasok").toString());
	            call.execute();
            
            }
        	System.out.println("out = " + sql + "kode :" + kode);       
        	sql = "{call bbtrx.SETID_PEMASOK(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("id_pemasok").toString());
            call.execute();
	
            sql = "{call bbtrx.SETKETERANGAN(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("keterangan").toString());
            call.execute();
            
            
            
            sql = "{call BBTRX.SETUPDATEBY(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("updateby").toString());
            call.execute();
            
            sql = "{call BBTRX.SETAPP_ACCESS(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("app_access").toString());
            call.execute();
            
            sql = "{? = call BBTRX.INSERTMASTERBB(?, ?)}";
            call = con.prepareCall(sql);
            call.registerOutParameter(1, OracleTypes.VARCHAR);
            call.setString(2, kode);
            call.setString(3, map.get("crud").toString());       
            call.execute();
            
            
        map.clear();    
        map.put("message", call.getString(1));    
        System.out.println(call.getString(1));
    }catch (Exception e) {
    	map.put("message", e.getMessage());    
    	e.printStackTrace();
	}
    lMap.add(map);        
    return lMap;
	
	}
	
}
